﻿Public Class frmGeo
    '注明。本来long表示经度，纬表示纬度。本程序表示反了，成了lat表示经度，log表示纬度。但整个程序的计算结果是正确的。
    Public UTMPoint As New UTM.UTMPoint
    Public GeoPoint As New UTM.GeoPoint
    Public Zone As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim latdd As Double
        Dim logdd As Double
        If Me.cmbDDEW.SelectedIndex = 0 Then
            latdd = txtLatDD.Value
            Zone = 31 + Math.Truncate(txtLatDD.Value / 6)
        ElseIf Me.cmbDDEW.SelectedIndex = 1 Then
            latdd = 0 - txtLatDD.Value
            Zone = 30 - Math.Truncate(txtLatDD.Value / 6)
        End If
        If Me.cmbDDNS.SelectedIndex = 0 Then
            logdd = txtLogDD.Value
        ElseIf Me.cmbDDNS.SelectedIndex = 1 Then
            logdd = 0 - txtLogDD.Value
        End If

        Dim Cov As New UTM.Cover
        UTMPoint = Cov.GeoToUtm(latdd, logdd, 32600 + Zone)
        DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        txtLatDD.Enabled = RadioButton2.Checked
        cmbDDEW.Enabled = RadioButton2.Checked
        txtLogDD.Enabled = RadioButton2.Checked
        'cmbDDNS.Enabled = RadioButton2.Checked

        txtLatD.Enabled = RadioButton1.Checked
        txtLatM.Enabled = RadioButton1.Checked
        txtLatS.Enabled = RadioButton1.Checked
        cmbDMSEW.Enabled = RadioButton1.Checked

        txtLogD.Enabled = RadioButton1.Checked
        txtLogM.Enabled = RadioButton1.Checked
        txtLogS.Enabled = RadioButton1.Checked
        'cmbDMSNS.Enabled = RadioButton1.Checked
    End Sub


    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        txtLatDD.Enabled = RadioButton2.Checked
        cmbDDEW.Enabled = RadioButton2.Checked
        txtLogDD.Enabled = RadioButton2.Checked
        'cmbDDNS.Enabled = RadioButton2.Checked

        txtLatD.Enabled = RadioButton1.Checked
        txtLatM.Enabled = RadioButton1.Checked
        txtLatS.Enabled = RadioButton1.Checked
        cmbDMSEW.Enabled = RadioButton1.Checked

        txtLogD.Enabled = RadioButton1.Checked
        txtLogM.Enabled = RadioButton1.Checked
        txtLogS.Enabled = RadioButton1.Checked
        'cmbDMSNS.Enabled = RadioButton1.Checked
    End Sub
    Private Sub DDtoDMS()
        Dim dd As Double = txtLatDD.Value
        txtLatD.Value = Math.Truncate(dd)
        Dim mm As Double = 60 * (dd - Math.Truncate(dd))
        txtLatM.Value = Math.Truncate(mm)
        Dim ss As Double = mm - Math.Truncate(mm)
        txtLatS.Value = 60 * ss

        dd = txtLogDD.Value
        txtLogD.Value = Math.Truncate(dd)
        mm = 60 * (dd - Math.Truncate(dd))
        txtLogM.Value = Math.Truncate(mm)
        ss = mm - Math.Truncate(mm)
        txtLogS.Value = 60 * ss
    End Sub
    Private Sub DMStoDD()

        Dim dd As Double = txtLatD.Value
        Dim mm As Double = txtLatM.Value / 60
        Dim ss As Double = txtLatS.Value / 60 / 60
        txtLatDD.Value = dd + mm + ss

        dd = txtLogD.Value
        mm = txtLogM.Value / 60
        ss = txtLogS.Value / 60 / 60
        txtLogDD.Value = dd + mm + ss
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Close()
    End Sub

    Private Sub txtLatD_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtLatD.Validating, txtLatM.Validating, txtLatS.Validating, txtLogD.Validating, txtLogM.Validating, txtLogS.Validating
        DMStoDD()
    End Sub


    Private Sub txtLatDD_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtLatDD.Validating, txtLogDD.Validating
        DDtoDMS()

    End Sub

    Private Sub frmGeo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Zone > 30 Then
            Me.cmbDDEW.SelectedIndex = 0
        Else
            Me.cmbDDEW.SelectedIndex = 1
        End If
        Me.cmbDDNS.SelectedIndex = 0
        Me.cmbDMSNS.SelectedIndex = 0
        Dim Cov As New UTM.Cover
        GeoPoint = Cov.UtmToGeo(UTMPoint.x, UTMPoint.y, 32600 + Zone)
        txtLatDD.Value = GeoPoint.x
        txtLogDD.Value = GeoPoint.y
        DDtoDMS()
    End Sub

    Private Sub cmbDDEW_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDDEW.SelectedIndexChanged
        cmbDMSEW.SelectedIndex = cmbDDEW.SelectedIndex
    End Sub

    Private Sub cmbDMSEW_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDMSEW.SelectedIndexChanged
        cmbDDEW.SelectedIndex = cmbDMSEW.SelectedIndex
    End Sub
End Class